package com.jiang.jetpackdemo.adapter

import android.content.Context
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.model.NewsInfo

/**
 *
 * @ProjectName:    JetpackDemo
 * @ClassName:      News2Adapter
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/4/6 16:31
 */
class News2Adapter(context: Context, list: MutableList<NewsInfo>, layoutId: Int) :
    BaseAdapter<NewsInfo, News2Adapter.ViewHolder>(context, list, layoutId) {
    class ViewHolder(itemView: View) : BaseAdapter.ViewHolder(itemView) {
        val iv: ImageView = itemView.findViewById(R.id.iv_pic)
        val tvT: TextView = itemView.findViewById(R.id.tv_t)
        val tvD: TextView = itemView.findViewById(R.id.tv_d)
        val btn: Button = itemView.findViewById(R.id.btn)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val newsInfo = list[position]
        holder.tvT.text = newsInfo.title
        holder.tvD.text = newsInfo.des
        Glide.with(context)
            .load(newsInfo.imgUrl)
            .placeholder(loadRes)
            .error(errRes)
            .into(holder.iv)
        holder.btn.setOnClickListener { onClickListener?.onClick(position) }
    }

    override fun getViewHolder(rootView: View): ViewHolder = ViewHolder(rootView)
}